package com.ruoyi.system.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.UUID;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.SinSincerity;
import com.ruoyi.system.service.ISinSincerityService;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.SinDeedMapper;
import com.ruoyi.system.domain.SinDeed;
import com.ruoyi.system.service.ISinDeedService;

/**
 * 事迹分值Service业务层处理
 *
 * @author buke
 * @date 2023-03-10
 */
@Service
public class SinDeedServiceImpl implements ISinDeedService
{
    @Autowired
    private SinDeedMapper sinDeedMapper;

    @Autowired
    private ISysUserService userService ;

    @Autowired
    private ISinSincerityService sincerityService ;


    /**
     * 查询事迹分值
     *
     * @param id 事迹分值主键
     * @return 事迹分值
     */
    @Override
    public SinDeed selectSinDeedById(Long id)
    {
        return sinDeedMapper.selectSinDeedById(id);
    }

    /**
     * 查询事迹分值列表
     *
     * @param sinDeed 事迹分值
     * @return 事迹分值
     */
    @Override
    public List<SinDeed> selectSinDeedList(SinDeed sinDeed)
    {
        return sinDeedMapper.selectSinDeedList(sinDeed);
    }

    /**
     * 新增事迹分值
     *
     * @param sinDeed 事迹分值
     * @return 结果
     */
    @Override
    public int insertSinDeed(SinDeed sinDeed)
    {
        sinDeed.setCreateTime(DateUtils.getNowDate());
        if (!Objects.isNull(sinDeed.getParentId()) && sinDeed.getParentId() != 0L) {
            SinDeed deed = this.selectSinDeedById(sinDeed.getParentId());
            sinDeed.setParentDescribe(deed.getName());
        }
        return sinDeedMapper.insertSinDeed(sinDeed);
    }

    /**
     * 修改事迹分值
     *
     * @param sinDeed 事迹分值
     * @return 结果
     */
    @Override
    public int updateSinDeed(SinDeed sinDeed)
    {
        sinDeed.setUpdateTime(DateUtils.getNowDate());
        return sinDeedMapper.updateSinDeed(sinDeed);
    }

    /**
     * 批量删除事迹分值
     *
     * @param ids 需要删除的事迹分值主键
     * @return 结果
     */
    @Override
    public int deleteSinDeedByIds(Long[] ids)
    {
        return sinDeedMapper.deleteSinDeedByIds(ids);
    }

    /**
     * 删除事迹分值信息
     *
     * @param id 事迹分值主键
     * @return 结果
     */
    @Override
    public int deleteSinDeedById(Long id)
    {
        return sinDeedMapper.deleteSinDeedById(id);
    }

    @Override
    public List<SinDeed> selectSinChildDeedList() {
        return sinDeedMapper.selectSinChildDeedList() ;
    }

    @Override
    public void addData() {
        List<SysUser> users = userService.selectUserList(new SysUser());
        List<SinDeed> deeds = sinDeedMapper.selectSinChildDeedList();
        for (int i = 0; i < 50; i++) {
            for (SysUser user : users) {
                for (SinDeed deed : deeds) {
                    SinSincerity sincerity = new SinSincerity();
                    sincerity.setMainUser(user.getNickName());
                    sincerity.setMainUserId(user.getUserId());
                    sincerity.setSincerityType(deed.getType());
                    sincerity.setSincerityTypeName(deed.getName());
                    sincerity.setSincerityTypeId(deed.getId());
                    sincerity.setSincerityNumber(deed.getScoreNumber());
                    sincerity.setTitle("诚信事迹");
                    sincerity.setContent(UUID.randomUUID().toString());
                    sincerity.setCreateTime(new Date());
                    sincerityService.insertSinSincerity(sincerity) ;
                }
            }
        }
    }
}
